package com.feng.test.pdf;

/**
 * @author HuangSuip
 */

public class PDF2Image {/*

    public static void main(String[] args) {
        // read/store the font cache.
        //FontPropertiesManager.getInstance().loadOrReadSystemFonts();
        PDF2Image pdf = new PDF2Image();
        pdf.capturePages("E:\\word\\004.pdf");
    }

    public void capturePages(String filePath) {
        // open the url
        Document document = new Document();

        // setup two threads to handle image extraction.
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        try {
            document.setFile(filePath);
            // create a list of callables.
            int pages = document.getNumberOfPages();
            java.util.List<Callable<Void>> callables = new ArrayList<>(pages);
            for (int i = 0; i < pages; i++) {
                callables.add(new CapturePage(document, i));
            }
            executorService.invokeAll(callables);
            executorService.submit(new DocumentCloser(document)).get();

        } catch (InterruptedException | ExecutionException | PDFException e) {
            System.out.println("Error parsing PDF document " + e);
        } catch (PDFSecurityException ex) {
            System.out.println("Error encryption not supported " + ex);
        } catch (FileNotFoundException ex) {
            System.out.println("Error file not found " + ex);
        } catch (IOException ex) {
            System.out.println("Error handling PDF document " + ex);
        }
        executorService.shutdown();
    }

    *//**
     * Captures images found in a page  parse to file.
     *//*
    public class CapturePage implements Callable<Void> {
        private Document document;
        private int pageNumber;
        private float scale = 1.5f;
        private float rotation = 0f;

        private CapturePage(Document document, int pageNumber) {
            this.document = document;
            this.pageNumber = pageNumber;
        }

        public Void call() {
            try {
                Page page = document.getPageTree().getPage(pageNumber);
                page.init();
                PDimension sz = page.getSize(Page.BOUNDARY_CROPBOX, rotation, scale);

                int pageWidth = (int) sz.getWidth();
                int pageHeight = (int) sz.getHeight();

                BufferedImage image = new BufferedImage(
                    pageWidth,
                    pageHeight,
                    BufferedImage.TYPE_INT_ARGB);
                Graphics g = image.createGraphics();

                page.paint(g, GraphicsRenderingHints.PRINT,
                    Page.BOUNDARY_CROPBOX, rotation, scale);
                g.dispose();
                // capture the page image to file

                System.out.println("Capturing page " + pageNumber);
                File file = new File("imageCapture_" + pageNumber + ".png");
                ImageIO.write(image, "png", file);

                image.flush();

            } catch (Throwable e) {
                e.printStackTrace();
            }

            return null;
        }
    }

    *//**
     * Disposes the document.
     *//*
    public class DocumentCloser implements Callable<Void> {
        private Document document;

        private DocumentCloser(Document document) {
            this.document = document;
        }

        public Void call() {
            if (document != null) {
                document.dispose();
                System.out.println("Document disposed");
            }
            return null;
        }
    }
*/}
